package com.arcsoft.arcfacesingle.charge.server.controller;

import com.arcsoft.arcfacesingle.charge.server.model.request.*;
import com.arcsoft.arcfacesingle.charge.server.model.response.BaseResponse;
import com.arcsoft.arcfacesingle.charge.server.util.FileUtils;
import com.arcsoft.arcfacesingle.charge.server.util.ResponseFactory;
import com.google.gson.Gson;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * API服务类，模拟实现接口文档中3.1、3.2、3.3、3.4四个接口，供设备端APP调用
 *
 * @author
 * @date 2020-01-08
 */
@RestController
public class ApiController {

    /**
     * 模拟实现接口文档中3.1接口
     * @param requestJson 接收到的请求串
     * @return 返回结果，JSON格式
     */
    @RequestMapping(value = "/attendance", method = RequestMethod.POST)
    public String attendance(@RequestBody String requestJson) {
        Gson gson = new Gson();
        if (requestJson == null || requestJson.isEmpty()) {
            BaseResponse response = ResponseFactory.createFailedResponse();
            return gson.toJson(response);
        }
        RecordList list = gson.fromJson(requestJson, RecordList.class);
        if (!checkParam(list)) {
            BaseResponse response = ResponseFactory.createFailedResponse();
            return gson.toJson(response);
        }
        List<Record> records = list.getRecordList();
        if (records == null) {
            BaseResponse response = ResponseFactory.createFailedResponse();
            return gson.toJson(response);
        }
        Map<String, Integer> map = new HashMap<>();
        for (Record record : records) {
            if (saveData(record)) {
                map.put(record.getEquipmentVerificationId(), 1);
            } else {
                map.put(record.getEquipmentVerificationId(), 0);
            }
        }
        BaseResponse response = ResponseFactory.createSuccessResponse();
        response.setData(map);
        String msgResp = new Gson().toJson(response);
        System.out.println("attendance:" + msgResp);
        return  msgResp;
    }

    /**
     * 模拟实现接口文档中3.2接口
     * @param requestJson 接收到的请求串
     * @return 返回结果，JSON格式
     */
    @RequestMapping(value = "/attendanceimage", method = RequestMethod.POST)
    public String attendanceImage(@RequestBody String requestJson) {
        Gson gson = new Gson();
        if (requestJson == null || requestJson.isEmpty()) {
            BaseResponse response = ResponseFactory.createFailedResponse();
            return gson.toJson(response);
        }
        RecordImageList list = gson.fromJson(requestJson, RecordImageList.class);
        if (!checkParam(list)) {
            BaseResponse response = ResponseFactory.createFailedResponse();
            return gson.toJson(response);
        }
        List<RecordImage> records = list.getRecordList();
        if (records == null) {
            BaseResponse response = ResponseFactory.createFailedResponse();
            return gson.toJson(response);
        }
        Map<String, Integer> map = new HashMap<>();
        for (RecordImage image : records) {
            String base64 = image.getImageBase64();
            FileUtils.saveImageBase64(base64, "arcFaceGoServer.jpg");
            if (saveData(image)) {
                map.put(image.getEquipmentVerificationId(), 1);
            } else {
                map.put(image.getEquipmentVerificationId(), 0);
            }
        }
        BaseResponse response = ResponseFactory.createSuccessResponse();
        response.setData(map);
        String msgResp = new Gson().toJson(response);
        System.out.println("attendanceimage:" + msgResp);
        return  msgResp;
    }

    /**
     * 模拟实现接口文档中3.3接口
     * @param requestJson 接收到的请求串
     * @return 返回结果
     */
    @RequestMapping(value = "/heartbeat", method = RequestMethod.POST)
    public String heartbeat(@RequestBody String requestJson) {
        Gson gson = new Gson();
        if (requestJson == null || requestJson.isEmpty()) {
            BaseResponse response = ResponseFactory.createFailedResponse();
            return gson.toJson(response);
        }
        HeartBeat heartBeat = gson.fromJson(requestJson, HeartBeat.class);
        if (!checkParam(heartBeat)) {
            BaseResponse response = ResponseFactory.createFailedResponse();
            return gson.toJson(response);
        }
        if (!saveData(heartBeat)) {
            BaseResponse response = ResponseFactory.createFailedResponse();
            return gson.toJson(response);
        }
        BaseResponse response = ResponseFactory.createSuccessResponse();
        String json = gson.toJson(response);
        System.out.println("heartbeat:" + json);
        return json;
    }

    /**
     * 模拟实现接口文档中3.4接口
     * @param requestJson 接收到的请求串
     * @return 返回结果
     */
    @RequestMapping(value = "/attendance/icrecord", method = RequestMethod.POST)
    public String attendanceIcCard(@RequestBody String requestJson) {
        Gson gson = new Gson();
        if (requestJson == null || requestJson.isEmpty()) {
            BaseResponse response = ResponseFactory.createFailedResponse();
            return gson.toJson(response);
        }
        RecordList list = gson.fromJson(requestJson, RecordList.class);
        if (!checkParam(list)) {
            BaseResponse response = ResponseFactory.createFailedResponse();
            return gson.toJson(response);
        }
        List<Record> records = list.getRecordList();
        if (records == null) {
            BaseResponse response = ResponseFactory.createFailedResponse();
            return gson.toJson(response);
        }
        Map<String, Integer> map = new HashMap<>();
        for (Record record : records) {
            if (saveData(record)) {
                map.put(record.getEquipmentVerificationId(), 1);
            } else {
                map.put(record.getEquipmentVerificationId(), 0);
            }
        }
        BaseResponse response = ResponseFactory.createSuccessResponse();
        response.setData(map);
        String msgResp = new Gson().toJson(response);
        System.out.println("attendance/icrecord:" + msgResp);
        return  msgResp;
    }

    /**
     * 模拟数据保存方法
     * @param object
     * @return
     */
    public boolean saveData(Object object) {
        //TODO 在此实现数据保存逻辑，如：将数据保存至数据库或者本地文件中
        return true;
    }

    /**
     * 数据合法性等校验
     * @param object
     * @return
     */
    public boolean checkParam(Object object) {
        //TODO 在此实现合法性校验等逻辑
        return true;
    }
}
